<?php
namespace Admin\Controller;

use Admin\Model\PrivilegeModel;

class PrivilegeController extends AdminBaseController
{
	public function lst()
	{
		$model = new PrivilegeModel();

		$level = I('get.level', 1);
		$pid = I('get.pid', 0);
		//echo $pid;

		$data = $model->where(array('level'=>array('eq',$level),'parent_id'=>array('eq',$pid)))->order('id')->select();

		$this->assign(array(
			'data'	=>	$data,
			'level'	=>	$level+1,
			'pid'	=>	$pid,
			'leve'	=>	$level,
		));

		$this->display();
	}

    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/Privilege');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst', array('level'=>I('post.level', 0),'pid' => I('post.parent_id', 0))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		$this->display();
    }
    public function edit()
    {
		$model = new PrivilegeModel();

		if(IS_POST)
		{
			if($model->create(I('post.'), 2))
			{
				if($model->save() !== FALSE)
				{
					$this->success('修改成功！', U('lst', array('level'=>I('post.level', 0),'pid' => I('post.parent_id', 0))));
					exit;
				}
			}
			$this->error($model->getError());
		}

		$id = I('get.id');

		$privilege = $model->where('id = '.$id)->find();

		$this->assign(array(
			'pri'	=>	$privilege,
		));

		$this->display();
    }
    public function delete()
    {
		$model = new PrivilegeModel();

		$id = I('get.id',0);

		if($id == 0){
			$this->error("操作错误!");
		}else{
			$pris = $model->where('parent_id = '.$id)->select();
			if(!empty($pris)){
				$this->error("此节点有下级节点,无法删除!");
			}
		}

    	if($model->delete($id) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('level'=>I('get.level', 0),'pid' => I('get.pid', 0))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }

}